//
//  LWQSwichButtonView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/10.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "LWQSwichButtonView.h"

@implementation LWQSwichButtonView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame andArray:(NSMutableArray *)buttonArray{
    if ([super initWithFrame:frame]) {
        dataArray = buttonArray;
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColorChange colorwithHexString:@"#20B097"];
    [self addSubview:lineView];
    CGFloat W = (self.frame.size.width - 100)/dataArray.count;
    CGFloat X = (W-100)/2;
    for (int i  =0; i<dataArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100+W*i+X, 5,100, 30);
        [button setTitle:dataArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:lineView.backgroundColor forState:UIControlStateSelected];
        [button setTitleColor:TITLECOLOR forState:UIControlStateNormal];
        button.tag = 100+i;
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:button];
        if (i == 0) {
            button.selected = YES;
            lineView.frame = CGRectMake(0, 0, 90, 2);
            lineView.center = CGPointMake(button.center.x, button.center.y+20);
        }
    }
    [self addSubview:self.backButton];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@50);
        make.height.equalTo(@20);
    }];
}
- (void)uploadLayout{
    CGFloat W = (self.frame.size.width - 100)/dataArray.count;
    CGFloat X = (W-100)/2;
    for (int i  =0; i<dataArray.count; i++) {
        UIButton *button = (UIButton *)[self viewWithTag:i+100];
        button.frame = CGRectMake(100+W*i+X, 5,100, 30);
        if (button.selected == YES) {
            lineView.frame = CGRectMake(0, 0, 90, 2);
            lineView.center = CGPointMake(button.center.x, button.center.y+20);
        }
    }
}
- (void)buttonClick:(UIButton *)sender{
    for (int i = 0; i<dataArray.count; i++) {
        UIButton *button = (UIButton *)[self viewWithTag:100+i];
        button.selected = NO;
    }
    sender.selected = YES;
    [UIView animateWithDuration:0.5 animations:^{
        self->lineView.center = CGPointMake(sender.center.x, sender.center.y+20);
    }];
    [self.delegate scrollViewChangeButtom:(int)sender.tag-100];
    
}
- (UIButton *)backButton{
    if (!_backButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@" 返回" forState:UIControlStateNormal];
        [button setTitleColor:[UIColorChange colorwithHexString:@"SELECTEDCOLOR"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"back_color"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:SELECTEDCOLOR forState:0];
        [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        _backButton = button;
    }
    return _backButton;
}
- (void)back:(UIButton *)sender{
    [self.delegate back];
}

@end
